package src;

import javazoom.jl.player.Player;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

public class WarPanel extends JPanel {
    private static String LEFT="left";
    private static String RIGHT="right";
    private static String STATIC="static";
    String path=null;
    int x;
    int y;
    int bloodMan=430;
    int bloodMaster=413;
    int time=600;
    InputStream AsStream=this.getClass().getClassLoader().getResourceAsStream("music/victorymaster.mp3");
    InputStream AsStream1=this.getClass().getClassLoader().getResourceAsStream("music/victoryman.mp3");

    SuperMan superMan=new SuperMan();
    Matser matser=new Matser();
    public BufferedImage StreamToImage(String str){
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(str);
        BufferedImage bufferedImage=null;
        try {
            bufferedImage= ImageIO.read(resourceAsStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bufferedImage;

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);


        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("image/background1.jpg");
        BufferedImage read=null;
        try {
             read= ImageIO.read(resourceAsStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Image image = new ImageIcon(read).getImage();
        g.drawImage(image, 0, 0, 1024, 680, null);
        if(time>0) {
            if(bloodMan>0&&bloodMaster>0){
                BufferedImage image11 = StreamToImage("image/bloodliang.png");


                Image image1 = new ImageIcon(image11).getImage();

                g.drawImage(image1, 10, 20, null);
                g.drawImage(image1, 510, 20, null);


                drawSuperMan(g);
                drawMaster(g);
                drawblood(g);


                g.setColor(Color.BLUE);
                Font font1 = new Font("黑体", Font.BOLD, 20);
                g.setFont(font1);
                g.fillRect(44, 38, bloodMan, 20);

                g.setColor(Color.RED);
                Font font = new Font("黑体", Font.BOLD, 20);
                g.setFont(font);
                g.fillRect(550, 38, bloodMaster, 20);

                g.setColor(Color.CYAN);
                Font font2 = new Font("黑体", Font.BOLD, 20);
                g.setFont(font2);
                g.drawString("" + time / 10, 502, 100);

            }
            else if(bloodMan<=0&&bloodMaster>0)
            {
                g.setColor(Color.WHITE);
                Font font3 = new Font("黑体", Font.BOLD, 80);
                g.setFont(font3);
                g.drawString(" The red side ",200,300);
                g.drawString("won  the  game.",200,400);

                try {
                    if(AsStream == null)
                    {

                    }
                    else {


                        playmusic(AsStream);
                        AsStream=null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
            else if(bloodMaster<=0&&bloodMan>0)
            {
                g.setColor(Color.WHITE);
                Font font3 = new Font("黑体", Font.BOLD, 80);
                g.setFont(font3);
                g.drawString(" The blue side ",200,300);
                g.drawString("won   the   game.",200,400);

                try {
                    if(AsStream1 == null)
                    {

                    }
                    else {


                        playmusic(AsStream1);
                        AsStream1=null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }


        }
        else
        {
            if(bloodMaster>bloodMan)
            {
                g.setColor(Color.WHITE);
                Font font3 = new Font("黑体", Font.BOLD, 80);
                g.setFont(font3);
                g.drawString(" The red side ",200,300);
                g.drawString("won  the  game.",200,400);



                try {
                    if(AsStream == null)
                    {

                    }
                    else {


                        playmusic(AsStream);
                        AsStream=null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            else{
                g.setColor(Color.WHITE);
                Font font3 = new Font("黑体", Font.BOLD, 80);
                g.setFont(font3);
                g.drawString(" The blue    side ",200,300);
                g.drawString("won  the  game.",200,400);

                try {
                    if(AsStream1 == null)
                    {

                    }
                    else {


                        playmusic(AsStream1);
                        AsStream1=null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

        }

        time--;

    }

    public void drawblood(Graphics g)
    {

        if (path ==null) {


        }
        else
        {
            BufferedImage image11 = StreamToImage(path);
            Image image = new ImageIcon(image11).getImage();
            g.drawImage(image, x, y, null);

        }



    }

    private void drawMaster(Graphics g) {
        matser.drawMe(g);
    }

    private void drawSuperMan(Graphics g)
    {

        superMan.drawMe(g);

    }
    public void playmusic(InputStream inputStream) throws Exception{
//        BufferedInputStream bufferedInputStream=new BufferedInputStream(inputStream);
        Player player=new Player(inputStream);
        player.play();


    }

    public void manmove(int keyCode) {
        switch (keyCode){
            case KeyEvent.VK_A:
                superMan.setPose("left");
                break;
            case KeyEvent.VK_D:
                superMan.setPose("right");
                break;
            case KeyEvent.VK_J:
                if(superMan.getPose().equals("foot"))
                {

                }else{

                    superMan.setPose("hand");


                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/handhit.wav");
                    sound(resourceAsStream);

                }
            case KeyEvent.VK_K:
                if(superMan.getPose().equals("foot")||superMan.getPose().equals("hand"))
                {

                }else{
                    superMan.setPose("defense");
                }
                break;
            case KeyEvent.VK_L:
                if(superMan.getPose().equals("hand"))
                {

                }else{
                    superMan.setPose("foot");


                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/footman.wav");
                    sound(resourceAsStream);
                }


                break;


        }
    }
    public void mastermove(int keyCode) {
        switch (keyCode){
            case KeyEvent.VK_RIGHT:
                matser.setPose("right");
                break;
            case KeyEvent.VK_LEFT:
                matser.setPose("left");
                break;
            case KeyEvent.VK_NUMPAD1:
                if(matser.getPose().equals("hand"))
                {

                }else{
                    matser.setPose("foot");
                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/footmaster.wav");
                    sound(resourceAsStream);
                }


                break;
            case KeyEvent.VK_NUMPAD3:

                if(matser.getPose().equals("foot"))
                {

                }else{
                    matser.setPose("hand");
                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/hithand.wav");
                    sound(resourceAsStream);
                }
                break;
            case KeyEvent.VK_NUMPAD2:
                if(matser.getPose().equals("foot")||matser.getPose().equals("hand"))
                {

                }else{
                    matser.setPose("defense");
                }

                break;


        }
    }
    public void mastercancel(int keyCode) {
        switch(keyCode){
            case KeyEvent.VK_NUMPAD2:
                if(matser.getPose().equals("foot")||matser.getPose().equals("hand"))
                {

                }else{
                    matser.setPose(STATIC);
                }

                break;
        }

    }
    public void action(){
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                    hit();
                    repaint();


            }
        },20,100);//delay延迟20毫秒后，period每隔10毫秒执行一次run里面的内容

    }


    public void mancancel(int keyCode) {
        switch(keyCode){
            case KeyEvent.VK_K:
                if(superMan.getPose().equals("foot")||superMan.getPose().equals("hand"))
                {

                }else{
                    superMan.setPose(STATIC);

                }

                break;
        }
    }
    //碰撞检测
    int j=0;
    public void hit(){
        boolean toright1=superMan.toright;
        boolean toright2=matser.toright;

        Image image1;
        String imagePath1 = superMan.getImagePath();
        if(imagePath1==null){
            BufferedImage image11 = StreamToImage("master/toright1.png");
             image1= new ImageIcon(image11).getImage();


        }
        else{
            BufferedImage image11 = StreamToImage(imagePath1);
            image1 = new ImageIcon(image11).getImage();

        }


        int width1 = image1.getWidth(null);
        int height1 = image1.getHeight(null);
        int x1=superMan.getManx();
        int y1=superMan.getMany();
        String pose1 = superMan.getPose();


//        System.out.println(width1);
//        System.out.println(height1);
//        System.out.println(x1);
//        System.out.println(y1);


        String imagePath2 = matser.getImagePath();
        BufferedImage image12 = StreamToImage(imagePath2);
        Image image2 = new ImageIcon(image12).getImage();
        int width2 = image2.getWidth(null);
        int height2 = image2.getHeight(null);
        int x2=matser.getManx();
        int y2=matser.getMany();
        String pose2 = matser.getPose();
//        man的拳 master的静态
        if(pose1.equals("hand")&&pose2.equals("static"))
        {
           if(toright1)
           {
               if(x1+width1>x2&&x1+width1<x2+width2&&y1+height1/3>y2&&y1+height1/3<y2+height2)
               {

                   j++;

                   x=x1+width1;
                   y=y1+height1/5;
                   path="image/blood1.png";
                   InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/afterhit1.wav");
                   sound(resourceAsStream);
                   bloodMaster-=10;
                   x2+=5;
                   matser.setManx(x2);
                   if(j>5)
                   {
                       j=0;
                   }

               }
               else{
                   path=null;

               }

           }
           else
           {
               if(x1>x2&&x1<x2+width2&&y1+height1/3>y2&&y1+height1/3<y2+height2)
               {

                   j++;

                   x=x1;
                   y=y1+height1/5;
                   path= "image/blood1.png";


                   InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/afterhit1.wav");
                   sound(resourceAsStream);
                   x2-=5;
                   matser.setManx(x2);
                   bloodMaster-=10;
                   if(j>5)
                   {
                       j=0;
                   }

               }
               else{
                   path=null;

               }

           }




        }
//        man的脚 master的静态
        else if(pose1.equals("foot")&&pose2.equals("static")){


            if(toright1){
                if(x1+width1>x2&&x1+width1<x2+width2&&y1+height1/3>y2&&y1+height1/3<y2+height2) {
                    x = x1 + width1;
                    y = y1 + height1 *3/ 5;
                    path = "image/blood1.png";


                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/afterhit1.wav");
                    sound(resourceAsStream);
                    x2+=5;
                    matser.setManx(x2);
                    bloodMaster -= 8;
                }
                else{
                    path=null;
                }

            }else{
                if(x1>x2&&x1<x2+width2&&y1+height1/3>y2&&y1+height1/3<y2+height2) {
                    x = x1;
                    y = y1 + height1 *3/ 5;
                    path = "image/blood1.png";

                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/afterhit1.wav");
                    sound(resourceAsStream);
                    x2-=5;
                    matser.setManx(x2);
                    bloodMaster -= 8;
                }
                else{
                    path=null;
                }

            }



        }
//          master的拳 man的静态
        else if(pose2.equals("hand")&&pose1.equals("static"))
        {
            if(toright2){
                if(x2+width2>x1&&x2+width2<x1+width1&&y1+height1/3>y2&&y1+height1/3<y2+height2)
                {
                    x = x2+width2;
                    y = y2 + height1 / 5;
                    path = "image/blood1.png";


                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/afterhit.wav");
                    sound(resourceAsStream);
                    bloodMan -= 10;
                    x1+=3;
                    superMan.setManx(x1);
                }
                else
                {
                    path=null;
                }




            }
            else {
                if(x2>x1&&x2<x1+width1&&y1+height1/3>y2&&y1+height1/3<y2+height2)
                {
                    x = x2;
                    y = y2 + height1 / 5;
                    path = "image/blood1.png";


                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/afterhit2.wav");
                    sound(resourceAsStream);
                    bloodMan -= 10;
                    x1-=3;
                    superMan.setManx(x1);
                }
                else
                {
                    path=null;
                }

            }




        }


        else if(pose1.equals("static")&&pose2.equals("foot")){
            if(toright2)
            {
                if(x2+width2>x1&&x2+width2<x1+width1&&y1+height1/3>y2&&y1+height1/3<y2+height2)
                {
                    j++;

                    x=x2+width2;
                    y=y2+height1*3/5;
                    path="image/blood1.png";

                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/afterhit.wav");
                    sound(resourceAsStream);
                    bloodMan-=10;
                    x1+=5;
                    superMan.setManx(x1);
                    if(j>5)
                    {
                        j=0;
                    }


                }
                else{
                    path=null;
                }

            }
            else{
                if(x2>x1&&x2<x1+width1&&y1+height1/3>y2&&y1+height1/3<y2+height2)
                {
                    j++;

                    x=x2;
                    y=y2+height1*3/5;
                    path="image/blood1.png";
                    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/afterhit.wav");
                    sound(resourceAsStream);
                    bloodMan-=10;
                    x1-=5;
                    superMan.setManx(x1);
                    if(j>5)
                    {
                        j=0;
                    }


                }
                else{
                    path=null;
                }

            }




        }
        else{
            path=null;

        }





    }
    public void sound(InputStream inputStream)
    {
        try {


            AudioStream as=new
                    AudioStream(inputStream);
            AudioPlayer.player.start(as);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    public void sound(String str)
    {
        try {
            FileInputStream in = new FileInputStream(str);
            AudioStream as=new
                    AudioStream(in);
            AudioPlayer.player.start(as);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    public void restart(int keyCode) {
        if(time<=0||bloodMan<=0||bloodMaster<=0)
        switch(keyCode){
            case KeyEvent.VK_SHIFT:
                time=600;
                bloodMan=430;
                bloodMaster=413;


        }
    }
}
